package com.sansan.qiangji.realize.device;

import com.sansan.qiangji.VO.device.beltParam.BeltParamListResultVO;
import com.sansan.qiangji.VO.device.beltParam.BeltParamVO;
import com.sansan.qiangji.entity.device.BeltParam;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.device.beltParam.BeltParamAddForm;
import com.sansan.qiangji.form.device.beltParam.BeltParamEditForm;
import com.sansan.qiangji.form.device.beltParam.BeltParamListForm;
import com.sansan.qiangji.service.impl.device.BeltParamServiceImpl;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BeltParamReal {
    private BeltParamServiceImpl beltParamService;

    @Resource
    public void setBeltParamService(BeltParamServiceImpl beltParamService){
        this.beltParamService = beltParamService;
    }

    public BeltParamListResultVO beltParamList(BeltParamListForm beltParamListForm) {
        PageResult<BeltParam> beltParamPageResult = beltParamService.beltParamList(beltParamListForm);
        BeltParamListResultVO beltParamListResultVO = new BeltParamListResultVO();
        BeanUtils.copyProperties(beltParamPageResult, beltParamListResultVO);
        List<BeltParamVO> beltParamVOS = new ArrayList<>();
        for (BeltParam beltParam : beltParamPageResult.getContent()) {
            BeltParamVO beltParamVO = new BeltParamVO();
            BeanUtils.copyProperties(beltParam, beltParamVO);
            beltParamVOS.add(beltParamVO);
        }
        beltParamListResultVO.setBeltParamVOS(beltParamVOS);
        return beltParamListResultVO;
    }

    @Transactional
    public void addBeltParam(BeltParamAddForm beltParamAddForm) {
        List<BeltParam> beltParams = beltParamService.findBeltParamName(beltParamAddForm.getBeltParamName());
        if (beltParams.size() != 0) {
            log.error("【添加传送带参数】传送带参数已存在");
            throw new ServiceException(ResultEnum.BELT_PARAM_IS_EXIST);
        }
        List<String> beltParamTypeList = new ArrayList<>();
        beltParamTypeList.add("M");
        beltParamTypeList.add("D");
        if (beltParamTypeList.stream().noneMatch(e->e.equals(beltParamAddForm.getBeltParamType()))) {
            log.error("【添加传送带参数】传送带参数类型不存在");
            throw new ServiceException(ResultEnum.BELT_PARAM_TYPE_IS_NOT_EXIST);
        }
        BeltParam beltParam = new BeltParam();
        BeanUtils.copyProperties(beltParamAddForm, beltParam);
        beltParamService.addBeltParam(beltParam);
    }

    @Transactional
    public void editBeltParam(BeltParamEditForm beltParamEditForm) {
        List<String> beltParamTypeList = new ArrayList<>();
        beltParamTypeList.add("M");
        beltParamTypeList.add("D");
        if (beltParamTypeList.stream().noneMatch(e->e.equals(beltParamEditForm.getBeltParamType()))) {
            log.error("【编辑传送带参数】传送带参数类型不存在");
            throw new ServiceException(ResultEnum.BELT_PARAM_TYPE_IS_NOT_EXIST);
        }
        BeltParam beltParam = beltParamService.findById(beltParamEditForm.getBeltParamId());
        if (beltParam == null) {
            log.error("【编辑传送带参数】传送带参数不存在");
            throw new ServiceException(ResultEnum.BELT_PARAM_IS_NOT_EXIST);
        }
        if (!beltParam.getBeltParamName().equals(beltParamEditForm.getBeltParamName())) {
            List<BeltParam> beltParamList = beltParamService.findBeltParamName(beltParamEditForm.getBeltParamName());
            if (beltParamList.size() != 0) {
                log.error("【编辑传送带参数】传送带参数已存在");
                throw new ServiceException(ResultEnum.BELT_PARAM_IS_EXIST);
            }
        }
        BeanUtils.copyProperties(beltParamEditForm, beltParam);
        beltParamService.updateBeltParam(beltParam);
    }

    @Transactional
    public void deleteBeltParam(Integer beltParamId) {
        BeltParam beltParam = beltParamService.findById(beltParamId);
        if (beltParam == null) {
            log.error("【删除传送带参数】传送带参数不存在");
            throw new ServiceException(ResultEnum.BELT_PARAM_IS_NOT_EXIST);
        }
        beltParamService.deleteById(beltParamId);
    }
}
